home()
sleep(3000, 5000)
var c = text("Autox.js v6").findOnce().indexInParent()

yh()

for (var i = 0; i < 10; i++) {
    for (var n = 0; n < c; n++) {
        if (!text("Autox.js v6").exists()) {
            home()
            sleep(3000, 5000)
        }
        var t = text("Autox.js v6").findOnce().parent().child(n).text()
        // toastLog(t)
        if (t == "快手") {
            runs("ksgg", 1800)
            killedapp()
        }
        if (t == "快手极速版") {
            runs("kjgg", 1800)
            killedapp()
        }

        if (t == "喜番免费短剧") {
            runs("xf", 2400)
            killedapp()
        }

        if (t == "悟空浏览器") {
            runs("wkllq", 1800)
            killedapp()
        }

        if (t == "番茄畅听音乐版") {
            runs("fqctyyb", 900)
            killedapp()
        }

        if (t == "有柿") {
            runs("ys", 1500)
            killedapp()
        }

        if (t == "惊喜红包") {
            runs("jxhb", 1800)
            killedapp()
        }
        if (t == "UC浏览器极速版") {
            runs("ucjsb", 1800)
            killedapp()
        }

        if (t == "百度极速版") {
            runs("bdjsb", 1800)
            killedapp()
        }
        if (t == "百度大字版") {
            runs("bddzb", 1800)
            killedapp()
        }
        if (t == "今日头条极速版") {
            runs("jrtt", 1600)
            killedapp()
        }
        if (t == "云手机分身") {
            runs("wxllb", 1200)
            killedapp()
        }
        if (t == "Autox.js v6") {
            // toastLog("autojs之前任务完成")
            break
        }
    }
    toastLog("----第" + (i + 1) + "轮结束----")
    sleep(3000, 5000)
}


function yh() {

    if (text("快手").findOnce().indexInParent() < c) {
        runs("ksyh", 1800)
        killedapp()
        // engines.execScriptFile("./二快/" + "ksyh" + ".js")packageName("com.huawei.android.launcher")
        // let engineObj = engines.execScript('ksyh.js');
        // while (engineObj.getEngine() == null || !engineObj.getEngine().isDestroyed()) {
        //     sleep(3000);
        // }
    }
    if (text("快手极速版").findOnce().indexInParent() < c) {
        runs("kjyh", 1800)
        killedapp()
        // engines.execScriptFile("./二快/" + "kjyh" + ".js")
        // let engineObj = engines.execScript('kjyh.js');
        // while (engineObj.getEngine() == null || !engineObj.getEngine().isDestroyed()) {
        //     sleep(3000);
        // }
    }
}

function runs(nm, tm) {
    var starttime = new Date().getTime()
    var jiaob = engines.execScriptFile("./" + nm + ".js");
    // toastLog("==" + nm + "==开始运行")
    while (true) {
        sleep(30000)
        var endtime = new Date().getTime()
        var endstart = Math.floor((endtime - starttime) / 1000);
        if (endstart < tm) {
            if (jiaob.getEngine().isDestroyed()) {
                // toastLog("==" + nm + "==运行结束")
                sleep(3000, 5000)
                break
            }
        } else {
            jiaob.getEngine().forceStop()
            sleep(3000, 5000)
            toastLog("==" + nm + "==运行超时,强制退出")
            sleep(3000, 5000)
            break
        }
    }
}

// 关闭所有最近打开的应用
function killedapp() {
    recents()
    desc("关闭所有最近打开的应用").findOne(10000)
    sleep(3000)
    if (desc("关闭所有最近打开的应用").exists()) {
        var like = desc("关闭所有最近打开的应用").findOnce()
        randomClick(like);
        sleep(3000)
    }
}

// 控件-随机点击
function randomClick(kjobj) {
    var bound = kjobj.bounds();
    var randX = bound.left + random(1, bound.right - bound.left - 2);
    var randY = bound.top + random(1, bound.bottom - bound.top - 2);
    click(randX, randY)
}